package logic

import (
	"context"
	"elem_svc/models/cache"
	"elem_svc/models/mysql"
	"fmt"

	"elem_svc/elem"
	"elem_svc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type Login1Logic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLogin1Logic(ctx context.Context, svcCtx *svc.ServiceContext) *Login1Logic {
	return &Login1Logic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录1
func (l *Login1Logic) Login1(in *elem.Login1Request) (*elem.Login1Response, error) {
	// todo: add your logic here and delete this line

	get := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SendCode {
		return nil, fmt.Errorf("验证码错误,请重新输入")
	}

	var findUser mysql.User
	mysql.DB.Debug().Where("mobile = ?", in.Mobile).Find(&findUser)

	return &elem.Login1Response{
		Id: int64(findUser.ID),
	}, nil
}
